<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Helper\Helper;
use App\Http\Controllers\Controller as BaseController;
use App\Http\Logic\Admin\JobLogic;
use App\Http\Requests\JobRequest;
use App\Http\Requests\InterviewRequest;
use App\Common\Status;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psy\Exception\FatalErrorException;

class JobController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $params   = $request->query();
        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        $data     = JobLogic::getJobList($params, $pageSize, $page);
        return $this->success(Status::SUCCESS, 'success', $data);
    }


    /**
     * @param JobRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function createJob(JobRequest $request): JsonResponse
    {
        $params = $request->validated();
        $id     = JobLogic::createJob($params);
        $r      = $params['id'] ? [] : ['id' => $id];
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function dtlJob($id): JsonResponse
    {
        $info = JobLogic::getJobDtl($id);
        return $this->success(Status::SUCCESS, 'success', $info);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function delJob($id): JsonResponse
    {
        JobLogic::delJob($id);
        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * @param $id
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function stJob($id, Request $request): JsonResponse
    {
        $type   = $request->query("type");
        $status = $request->query("status");
        if ($type == 2) {
            if (!isset($status) || empty($status)) {
                //                throw new Exception("招聘状态必须");
                return $this->error(Status::ERROR, "招聘状态必须");
            }
        }
       $r = JobLogic::setStatus($id, $type, $status);
        if($r["status"] == Status::ERROR) {
            return $this->error(Status::ERROR, $r["msg"]);
        }
        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function autoMatch(Request $request)
    {
        $params   = $request->query();
        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        if (isset($params['position_category_id']) || isset($params['work_city']) || isset($params['salary']) ||
            isset($params['job_type']) || isset($params['education']) || isset($params['work_year'])) {
            if (!$params['jid']) {
                return $this->error(Status::ERROR, "手动匹配 必须指定招聘");
            }
        }
        $r = JobLogic::autoMatch($params, $pageSize, $page);

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * 匹配结果
     * @param $id
     * @return JsonResponse
     */
    public function mateResJob($id, Request $request)
    {
        $interview = $request->query("interview", "");
        //        $source   = $request->query("source", "");

        if (!$interview) {
            return $this->error(Status::ERROR, "查询信息有误");
        }
        $r = JobLogic::getMateResJob($id, $interview);
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * 修改匹配结果状态
     * @param $id
     * @param Request $request
     * @return JsonResponse
     */
    public function mateResJobSt($id, Request $request)
    {
        $type              = $request->query("type", "");
        $mobile            = $request->query("mobile", "");
        $reason            = $request->query("reason", "");
        $job_notes_auth_id = $request->query("job_notes_auth_id", "");
        $note_id = $request->query("note_id", "");
        $annex             = $request->query("annex", "");

        if (!$type || !$mobile) {
            return $this->error(Status::ERROR, "必传参数空");
        }
        $r = JobLogic::setMateResJobSt($id, $type, $mobile, $reason, $job_notes_auth_id, $annex, $note_id);

        if ($r["status"] == Status::SUCCESS) {
            return $this->success(Status::SUCCESS, 'success', []);
        }
        return $this->error(Status::ERROR, $r["msg"]);
    }


    /**
     * 获取用户详情
     * @param $mobile
     * @return JsonResponse
     */
    public function notesDtByMobile(Request $request)
    {
        $mobile = $request->query("mobile", "");
        $id     = $request->query("id", "");
        if (!$mobile && !$id) {
            return $this->error(Status::ERROR, "用户信息有误");
        }
        $r = JobLogic::getNotesDtByMobile($mobile, $id);
        return $this->success(Status::SUCCESS, 'success', $r);
    }

    /**
     * 邀请面试
     * @param InterviewRequest $request
     * @return JsonResponse
     */
    public function inviteInterview(InterviewRequest $request)
    {
        $params = $request->validated();

        $r = JobLogic::setInterview($params);

        if ($r['status'] == Status::ERROR) {
            return $this->error(Status::ERROR, $r['msg']);
        }
        return $this->success(Status::SUCCESS, $r['msg'], []);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function notesDownload($id)
    {
        JobLogic::downloadNote($id);
    }
}
